/**
 * 该文件实现全局认证，结合《统一认证》接口
 * https://common.jzdoctor.com/base/generated_ak.json
 * Created by yangyao on 2017/7/18.
 */
import JzHttpModule from './JzHttpModuleV1'
// import EcHttpUtils from '../EcHttpUtils'
import Constants from './Constants'
// import Vue from 'vue'

const oAuthPropsCacheKey = 'JZoAuthPropsCacheKey'
const oAuthAccessTokenCacheKey = 'JZoAuthAccessTokenCacheKey'

export default {
  oAuthPropsCacheKey: oAuthPropsCacheKey,
  oAuthAccessTokenCacheKey: oAuthAccessTokenCacheKey,
  /**
   * 获取缓存的accessToken
   */
  getAccessToken () {
    let ak = window.localStorage.getItem(oAuthAccessTokenCacheKey)
    return ak
  },
  /**
   * 获取当前最新参数
   */
  getProps () {
    var props = window.localStorage.getItem(oAuthPropsCacheKey)
    if (props != null && props !== '') {
      return JSON.parse(props)
    }
    return props
  },
  /**
   * 设置access_token无效
   */
  invalidAccessToken () {
    window.localStorage.removeItem(oAuthAccessTokenCacheKey)
  },
  payPost: function (URL, PARAMS) {
    var form = document.createElement('form')
    form.action = URL
    form.method = 'get'
    form.style.display = 'none'
    for (var x in PARAMS) {
      let hidden = document.createElement('input')
      hidden.setAttribute('type', 'hidden')
      hidden.setAttribute('name', x)
      hidden.setAttribute('value', PARAMS[x])
      form.appendChild(hidden)
    }
    document.body.appendChild(form)
    form.submit()
  },
  /**
   * 认证方法
   * @param props
   * @param ve 当前vue对象
   */
  aliyoauth (props) {
    if ((this.getAccessToken() !== null && this.getAccessToken() !== '')) {
      console.log('token已经存在，不再重新获取')
      return false
    }
    props = {
      appid: Constants.CommonApiUrl.aliPayAppid,
      app_type: Constants.CommonApiUrl.aliPayApp_type
    }
    var xhr = new XMLHttpRequest()
    if (process.env.NODE_ENV !== 'development') {
      xhr.withCredentials = true
    }
    xhr.open('POST', Constants.CommonApiUrl.TOKEN, false)
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && (xhr.status === 200)) {
        let resp = JSON.parse(xhr.responseText)
        let ak = resp.data.access_token
        // let ak = 'sss'
        console.log(ak)
        window.localStorage.setItem(oAuthPropsCacheKey, JSON.stringify(props))
        window.localStorage.setItem(oAuthAccessTokenCacheKey, ak)
        JzHttpModule.pushBaseParam('access_token', ak)
      }
    }
    xhr.send('appid=' + props.appid + '&app_type=' + props.app_type)
  },
  oauth (props) {
    let URL = Constants.CommonApiUrl.OATUTH + '/authorize'
    let DATA = {
      redirect_url: encodeURIComponent(window.location.href),
      appid: Constants.APPID,
      cookie: true
    }
    this.payPost(URL, DATA)
    // console.log('======', this.getAccessToken())
    // EcHttpUtils.get()
    // 支付宝不缓存
    // if ((this.getAccessToken() !== null && this.getAccessToken() !== '') && Vue.prototype.$payWay !== 200) {
    //   console.log('token已经存在，不再重新获取')
    //   return false
    // }
    // let appid = ''
    // let appType = ''
    // let unionid = ''
    // let mobileNo = ''
    // if (props) {
    //   appid = props.appid || ''
    //   appType = props.app_type || ''
    //   unionid = props.unionid || ''
    //   mobileNo = props.mobile_no || ''
    //   window.localStorage.setItem(oAuthPropsCacheKey, JSON.stringify(props))
    //   window.localStorage.setItem(oAuthAccessTokenCacheKey, '')
    //   JzHttpModule.pushBaseParam('access_token', '')
    // } else {
    //   props = JSON.parse(window.localStorage.getItem(oAuthPropsCacheKey))
    //   appid = props.appid || ''
    //   appType = props.app_type || ''
    //   unionid = props.unionid || ''
    //   mobileNo = props.mobile_no || ''
    // }
    // // console.log('开始ACCESS_TOKEN认证====', props)
    // if (Vue.prototype.$payWay === 200) {
    //   props = {}
    //   props = {
    //     appid: Constants.CommonApiUrl.aliPayAppid,
    //     app_type: Constants.CommonApiUrl.aliPayApp_type
    //   }
    // }
    // // console.log('开始ACCESS_TOKEN认证====', props)
    // // console.log(Constants.CommonApiUrl.TOKEN)
    // // alert(222)
    // var xhr = new XMLHttpRequest()
    // xhr.open('POST', Constants.CommonApiUrl.TOKEN, false)
    // xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    // xhr.onreadystatechange = function () {
    //   if (xhr.readyState === 4 && (xhr.status === 200)) {
    //     let resp = JSON.parse(xhr.responseText)
    //     let ak = resp.data.access_token
    //     // alert(JSON.stringify(ak))
    //     // console.log(222222)
    //     // console.log(ak)
    //     window.localStorage.setItem(oAuthPropsCacheKey, JSON.stringify(props))
    //     window.localStorage.setItem(oAuthAccessTokenCacheKey, ak)
    //     JzHttpModule.pushBaseParam('access_token', ak)
    //     // // 强制刷新页面
    //     // let thisurl = window.localStorage.getItem('thisUrl')
    //     // window.location.href = JSON.parse(thisurl)
    //     // window.location.reload(true)
    //     // if (window.location.href.indexOf('?') === -1) {
    //     //   window.location.href = window.location.href + '?t=' + new Date().getTime()
    //     // } else {
    //     //   window.location.href = window.location.href + '&t=' + new Date().getTime()
    //     // }
    //   }
    // }
    // 支付宝 + 微信
    // if (Vue.prototype.$payWay === 200) {
    //   xhr.send('appid=' + appid + '&app_type=' + appType)
    // } else {
    //   xhr.send('appid=' + appid + '&app_type=' + appType + '&unionid=' + unionid + '&mobile_no=' + mobileNo)
    // }
  }
}

